<?php

/**
 * ECSHOP 商品详情
 * ============================================================================
 * * 版权所有 2005-2012 上海商派网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.ecshop.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: liubo $
 * $Id: goods.php 17217 2011-01-19 06:29:08Z liubo $
*/

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');

if ((DEBUG_MODE & 2) != 2)
{
    $smarty->caching = true;
}

$affiliate = unserialize($GLOBALS['_CFG']['affiliate']);
$smarty->assign('affiliate', $affiliate);

/*------------------------------------------------------ */
//-- INPUT
/*------------------------------------------------------ */
//参数处理
$goods_id = isset($_REQUEST['id'])  ? intval($_REQUEST['id']) : 0; //稿件ID
$act_id = isset($_REQUEST['act_id'])  ? intval($_REQUEST['act_id']) : 0; //套装稿件ID
$cat_id = isset($_REQUEST['cat_id'])  ? intval($_REQUEST['cat_id']) : 0; //稿件分类ID
$t   = isset($_REQUEST['t'])  ? intval($_REQUEST['t']) : 0;//精品、套装、对比 三种类型：0、1、2
$v   = isset($_REQUEST['v'])  ? intval($_REQUEST['v']) : 0;//1：不显示缩略图列表

$goods_ids = '';
if($t==2){
	$goods_ids = getCookie_goods();
}
/*------------------------------------------------------ */
//-- PROCESSOR
/*------------------------------------------------------ */


$cache_id = 'goods2'.$t.'-'.$goods_ids.'-'.$act_id.'-'.$goods_id . '-'.$cat_id.'-' . $_SESSION['user_rank'].'-'.$_CFG['lang'].$v;
$cache_id = sprintf('%X', crc32($cache_id));
if (!$smarty->is_cached('goods2.dwt', $cache_id))
{
    $smarty->assign('image_width',  $_CFG['image_width']);
    $smarty->assign('image_height', $_CFG['image_height']);
    $smarty->assign('helps',        get_shop_help()); // 网店帮助
    $smarty->assign('id',           $goods_id);
	$smarty->assign('type',			0 );
    $smarty->assign('t',			$t);
    $smarty->assign('cfg',          $_CFG);
	$smarty->assign('v',			$v);
	$smarty->assign('act_id',			$act_id);

	$goodslist = false;
	$goods     = false;
	$cat2_id    = 0 ; //稿件行业ID
	if($cat_id>0 && $goods_id <=0){
		$goodslist = category_get_goods($cat_id);
	}else if($t == 1 && $act_id>0 ){
		$goodslist = category_get_goods1($act_id);
		$act_type = get_act_type($act_id);
		$smarty->assign('package_list',	get_package_list($act_type ,$goodslist ));
		//print_r(get_package_list($act_type ,$goodslist ));exit;
	}else if($t==2&&$goods_ids!=''){
		$goodslist = category_get_goods2($goods_ids);
	}

	if($goods_id>0){
		/* 获得商品的信息 */
		$goods = get_goods_info($goods_id , 1);
	}else{
		//从稿件列表获取第一个稿件
		if(count($goodslist)>0){
			$k = 0 ;
			foreach($goodslist as $key=>$da){
				if($k == 0){
					$k++;
					$goods_id = $da['goods_id'];
				}
			}
			if($goods_id>0){
				$goods = get_goods_info($goods_id , 1);
			}
		}
	}

    if ($goods === false)
    {
        /* 如果没有找到任何记录则跳回到首页 */
        ecs_header("Location: ./\n");
        exit;
    }
    else
    {	

		$cat_id = $goods['cat_id'];

		$cat  = get_cat_info($cat_id);   // 所属商品信息
		$cat2 = get_cat_info2($goods['cat2_id']); //适用行业信息
		$des  = get_des_info2($goods['des_id']);//设计师信息
        $smarty->assign('goods2_cat',		$cat);
        $smarty->assign('goods2_cat2',		$cat2);
		$smarty->assign('des',		$des);

        $shop_price   = $goods['shop_price'];
        $linked_goods = get_linked_goods($goods_id);

        $goods['goods_style_name'] = add_style($goods['goods_name'], $goods['goods_name_style']);

        $smarty->assign('goods',              $goods);
        $smarty->assign('goods_id',           $goods['goods_id']);
        $smarty->assign('promote_end_time',   $goods['gmt_end_time']);
        $smarty->assign('categories',         get_categories_tree());  // 分类树
		
        /* meta */
        $smarty->assign('keywords',           htmlspecialchars($goods['keywords']));
        $smarty->assign('description',        htmlspecialchars($goods['goods_brief']));

        $catlist = array();
        foreach(get_parent_cats($goods['cat_id']) as $k=>$v)
        {
            $catlist[] = $v['cat_id'];
        }
        assign_template('c', $catlist);

        $properties = get_goods_properties($goods_id);  // 获得商品的规格和属性
        $smarty->assign('properties',          $properties['pro']);                              // 商品属性
        $smarty->assign('specification',       $properties['spe']);                              // 商品规格
        $smarty->assign('rank_prices',         get_user_rank_prices($goods_id, $shop_price));    // 会员等级价格
        $smarty->assign('goods_rank',          get_goods_rank($goods_id));                       // 商品的销售排名
		$smarty->assign('t',				   $t);       
        //获取tag
        $tag_array = get_tags($goods_id);
        $smarty->assign('tags',                $tag_array);                                       // 商品的标记
		
		//同分类下的精品稿件列表
		if($t==0){
			$goodslist = category_get_goods($cat_id);
		}
		
		$k = 0 ;
		$cur_num = 0;
		foreach($goodslist as $dd){
			$k +=1;
			if($dd['goods_id'] == $goods_id){
				$cur_num = $k;
			}
		}
		$cur_page = ceil($cur_num/5)-1;
		$smarty->assign('cur_page',$cur_page);   //当前页数
		$smarty->assign('cur_num',$cur_num);   //当前页数
		
		$smarty->assign('goodslist',$goodslist);    // 商品优惠价格区间

		$smarty->assign('ur_here',         '<a href="category2.php">精品稿件</a> <code>></code> '.$goods['goods_name']);  // 当前位置

		if($cat_id>0){
			$smarty->assign('parent_id',				getCatParentID($cat_id));     //所属商品ID 
		}

		$smarty->assign('menu_index',		'3');//当前导航标识

		$smarty->assign('gallery_num',     count(get_goods_gallery($goods_id)));//相关相册相片个数

    }
}

/* 记录浏览历史 */
if (!empty($_COOKIE['ECS']['history']))
{
    $history = explode(',', $_COOKIE['ECS']['history']);

    array_unshift($history, $goods_id);
    $history = array_unique($history);

    while (count($history) > $_CFG['history_number'])
    {
        array_pop($history);
    }

    setcookie('ECS[history]', implode(',', $history), gmtime() + 3600 * 24 * 30);
}
else
{
    setcookie('ECS[history]', $goods_id, gmtime() + 3600 * 24 * 30);
}

$backurl = isset($_REQUEST['gid'])  ? 'goods.php?id='.$_REQUEST['gid'].'&id2='. $goods_id : 'javascript:window.history.go(-1);';
$smarty->assign('backurl',  $backurl);

/* 更新点击次数 */
$db->query('UPDATE ' . $ecs->table('goods_aa') . " SET act_count = (act_count + 1) WHERE act_id = '$act_id'");

$smarty->assign('now_time',  gmtime());           // 当前系统时间
$smarty->display('goods2.dwt',      $cache_id);

/*------------------------------------------------------ */
//-- PRIVATE FUNCTION
/*------------------------------------------------------ */

/**
 * 获得指定商品的关联商品
 *
 * @access  public
 * @param   integer     $goods_id
 * @return  array
 */
function get_linked_goods($goods_id)
{
    $sql = 'SELECT g.goods_id, g.goods_name, g.goods_thumb, g.goods_img, g.shop_price AS org_price, ' .
                "IFNULL(mp.user_price, g.shop_price * '$_SESSION[discount]') AS shop_price, ".
                'g.market_price, g.promote_price, g.promote_start_date, g.promote_end_date ' .
            'FROM ' . $GLOBALS['ecs']->table('link_goods') . ' lg ' .
            'LEFT JOIN ' . $GLOBALS['ecs']->table('goods') . ' AS g ON g.goods_id = lg.link_goods_id ' .
            "LEFT JOIN " . $GLOBALS['ecs']->table('member_price') . " AS mp ".
                    "ON mp.goods_id = g.goods_id AND mp.user_rank = '$_SESSION[user_rank]' ".
            "WHERE lg.goods_id = '$goods_id' AND g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 ".
            "LIMIT " . $GLOBALS['_CFG']['related_goods_number'];
    $res = $GLOBALS['db']->query($sql);

    $arr = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
        $arr[$row['goods_id']]['goods_id']     = $row['goods_id'];
        $arr[$row['goods_id']]['goods_name']   = $row['goods_name'];
        $arr[$row['goods_id']]['short_name']   = $GLOBALS['_CFG']['goods_name_length'] > 0 ?
            sub_str($row['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $row['goods_name'];
        $arr[$row['goods_id']]['goods_thumb']  = get_image_path($row['goods_id'], $row['goods_thumb'], true);
        $arr[$row['goods_id']]['goods_img']    = get_image_path($row['goods_id'], $row['goods_img']);
        $arr[$row['goods_id']]['market_price'] = price_format($row['market_price']);
        $arr[$row['goods_id']]['shop_price']   = price_format($row['shop_price']);
        $arr[$row['goods_id']]['url']          = build_uri('goods', array('gid'=>$row['goods_id']), $row['goods_name']);

        if ($row['promote_price'] > 0)
        {
            $arr[$row['goods_id']]['promote_price'] = bargain_price($row['promote_price'], $row['promote_start_date'], $row['promote_end_date']);
            $arr[$row['goods_id']]['formated_promote_price'] = price_format($arr[$row['goods_id']]['promote_price']);
        }
        else
        {
            $arr[$row['goods_id']]['promote_price'] = 0;
        }
    }

    return $arr;
}

/**
 * 获得指定商品的关联文章
 *
 * @access  public
 * @param   integer     $goods_id
 * @return  void
 */
function get_linked_articles($goods_id)
{
    $sql = 'SELECT a.article_id, a.title, a.file_url, a.open_type, a.add_time ' .
            'FROM ' . $GLOBALS['ecs']->table('goods_article') . ' AS g, ' .
                $GLOBALS['ecs']->table('article') . ' AS a ' .
            "WHERE g.article_id = a.article_id AND g.goods_id = '$goods_id' AND a.is_open = 1 " .
            'ORDER BY a.add_time DESC';
    $res = $GLOBALS['db']->query($sql);

    $arr = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
        $row['url']         = $row['open_type'] != 1 ?
            build_uri('article', array('aid'=>$row['article_id']), $row['title']) : trim($row['file_url']);
        $row['add_time']    = local_date($GLOBALS['_CFG']['date_format'], $row['add_time']);
        $row['short_title'] = $GLOBALS['_CFG']['article_title_length'] > 0 ?
            sub_str($row['title'], $GLOBALS['_CFG']['article_title_length']) : $row['title'];

        $arr[] = $row;
    }

    return $arr;
}

/**
 * 获得指定商品的各会员等级对应的价格
 *
 * @access  public
 * @param   integer     $goods_id
 * @return  array
 */
function get_user_rank_prices($goods_id, $shop_price)
{
    $sql = "SELECT rank_id, IFNULL(mp.user_price, r.discount * $shop_price / 100) AS price, r.rank_name, r.discount " .
            'FROM ' . $GLOBALS['ecs']->table('user_rank') . ' AS r ' .
            'LEFT JOIN ' . $GLOBALS['ecs']->table('member_price') . " AS mp ".
                "ON mp.goods_id = '$goods_id' AND mp.user_rank = r.rank_id " .
            "WHERE r.show_price = 1 OR r.rank_id = '$_SESSION[user_rank]'";
    $res = $GLOBALS['db']->query($sql);

    $arr = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {

        $arr[$row['rank_id']] = array(
                        'rank_name' => htmlspecialchars($row['rank_name']),
                        'price'     => price_format($row['price']));
    }

    return $arr;
}

/**
 * 获得购买过该商品的人还买过的商品
 *
 * @access  public
 * @param   integer     $goods_id
 * @return  array
 */
function get_also_bought($goods_id)
{
    $sql = 'SELECT COUNT(b.goods_id ) AS num, g.goods_id, g.goods_name, g.goods_thumb, g.goods_img, g.shop_price, g.promote_price, g.promote_start_date, g.promote_end_date ' .
            'FROM ' . $GLOBALS['ecs']->table('order_goods') . ' AS a ' .
            'LEFT JOIN ' . $GLOBALS['ecs']->table('order_goods') . ' AS b ON b.order_id = a.order_id ' .
            'LEFT JOIN ' . $GLOBALS['ecs']->table('goods') . ' AS g ON g.goods_id = b.goods_id ' .
            "WHERE a.goods_id = '$goods_id' AND b.goods_id <> '$goods_id' AND g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 " .
            'GROUP BY b.goods_id ' .
            'ORDER BY num DESC ' .
            'LIMIT ' . $GLOBALS['_CFG']['bought_goods'];
    $res = $GLOBALS['db']->query($sql);

    $key = 0;
    $arr = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
        $arr[$key]['goods_id']    = $row['goods_id'];
        $arr[$key]['goods_name']  = $row['goods_name'];
        $arr[$key]['short_name']  = $GLOBALS['_CFG']['goods_name_length'] > 0 ?
            sub_str($row['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $row['goods_name'];
        $arr[$key]['goods_thumb'] = get_image_path($row['goods_id'], $row['goods_thumb'], true);
        $arr[$key]['goods_img']   = get_image_path($row['goods_id'], $row['goods_img']);
        $arr[$key]['shop_price']  = price_format($row['shop_price']);
        $arr[$key]['url']         = build_uri('goods', array('gid'=>$row['goods_id']), $row['goods_name']);

        if ($row['promote_price'] > 0)
        {
            $arr[$key]['promote_price'] = bargain_price($row['promote_price'], $row['promote_start_date'], $row['promote_end_date']);
            $arr[$key]['formated_promote_price'] = price_format($arr[$key]['promote_price']);
        }
        else
        {
            $arr[$key]['promote_price'] = 0;
        }

        $key++;
    }

    return $arr;
}

/**
 * 获得指定商品的销售排名
 *
 * @access  public
 * @param   integer     $goods_id
 * @return  integer
 */
function get_goods_rank($goods_id)
{
    /* 统计时间段 */
    $period = intval($GLOBALS['_CFG']['top10_time']);
    if ($period == 1) // 一年
    {
        $ext = " AND o.add_time > '" . local_strtotime('-1 years') . "'";
    }
    elseif ($period == 2) // 半年
    {
        $ext = " AND o.add_time > '" . local_strtotime('-6 months') . "'";
    }
    elseif ($period == 3) // 三个月
    {
        $ext = " AND o.add_time > '" . local_strtotime('-3 months') . "'";
    }
    elseif ($period == 4) // 一个月
    {
        $ext = " AND o.add_time > '" . local_strtotime('-1 months') . "'";
    }
    else
    {
        $ext = '';
    }

    /* 查询该商品销量 */
    $sql = 'SELECT IFNULL(SUM(g.goods_number), 0) ' .
        'FROM ' . $GLOBALS['ecs']->table('order_info') . ' AS o, ' .
            $GLOBALS['ecs']->table('order_goods') . ' AS g ' .
        "WHERE o.order_id = g.order_id " .
        "AND o.order_status = '" . OS_CONFIRMED . "' " .
        "AND o.shipping_status " . db_create_in(array(SS_SHIPPED, SS_RECEIVED)) .
        " AND o.pay_status " . db_create_in(array(PS_PAYED, PS_PAYING)) .
        " AND g.goods_id = '$goods_id'" . $ext;
    $sales_count = $GLOBALS['db']->getOne($sql);

    if ($sales_count > 0)
    {
        /* 只有在商品销售量大于0时才去计算该商品的排行 */
        $sql = 'SELECT DISTINCT SUM(goods_number) AS num ' .
                'FROM ' . $GLOBALS['ecs']->table('order_info') . ' AS o, ' .
                    $GLOBALS['ecs']->table('order_goods') . ' AS g ' .
                "WHERE o.order_id = g.order_id " .
                "AND o.order_status = '" . OS_CONFIRMED . "' " .
                "AND o.shipping_status " . db_create_in(array(SS_SHIPPED, SS_RECEIVED)) .
                " AND o.pay_status " . db_create_in(array(PS_PAYED, PS_PAYING)) . $ext .
                " GROUP BY g.goods_id HAVING num > $sales_count";
        $res = $GLOBALS['db']->query($sql);

        $rank = $GLOBALS['db']->num_rows($res) + 1;

        if ($rank > 10)
        {
            $rank = 0;
        }
    }
    else
    {
        $rank = 0;
    }

    return $rank;
}

/**
 * 获得商品选定的属性的附加总价格
 *
 * @param   integer     $goods_id
 * @param   array       $attr
 *
 * @return  void
 */
function get_attr_amount($goods_id, $attr)
{
    $sql = "SELECT SUM(attr_price) FROM " . $GLOBALS['ecs']->table('goods_attr') .
        " WHERE goods_id='$goods_id' AND " . db_create_in($attr, 'goods_attr_id');

    return $GLOBALS['db']->getOne($sql);
}

/**
 * 获得分类的信息
 *
 * @param   integer $cat_id
 *
 * @return  void
 */
function get_cat_info($cat_id)
{
    return $GLOBALS['db']->getRow('SELECT cat_name, keywords, cat_desc, style, grade, filter_attr, parent_id FROM ' . $GLOBALS['ecs']->table('category') .
        " WHERE cat_id = '$cat_id'");
}
/**
 * 获得分类的信息
 *
 * @param   integer $cat_id
 *
 * @return  void
 */
function get_cat_info2($cat2_id)
{
    return $GLOBALS['db']->getRow('SELECT cat_name, keywords, cat_desc FROM ' . $GLOBALS['ecs']->table('category2') .
        " WHERE cat_id = '$cat2_id'");
}
/**
 * 获得设计师的信息
 *
 * @param   integer $cat_id
 *
 * @return  void
 */
function get_des_info2($des_id)
{
    return $GLOBALS['db']->getRow('SELECT * FROM ' . $GLOBALS['ecs']->table('designer') .
        " WHERE des_id = '$des_id'");
}

/**
 * 获得同分类下的精品稿件列表
 *
 * @access  public
 * @param   string  $children
 * @return  array
 */
function category_get_goods($cat_id = '', $size = 1000 , $page = 1)
{
    $display = $GLOBALS['display'];
    $where = "g.is_delete = 0 AND type = 1 AND g.cat_id = ".$cat_id;

    /* 获得商品列表 */
    $sql = 'SELECT g.goods_id,g.cat_id,g.des_id,g.cat2_id ,g.act_id,g.type, g.goods_name, g.goods_thumb , g.goods_img ' .
            'FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' .
            "WHERE $where  ORDER BY goods_id ASC";
    $res = $GLOBALS['db']->selectLimit($sql, $size, ($page - 1) * $size);

    $arr = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
		$arr[$row['goods_id']]['goods_id']         = $row['goods_id'];
        $arr[$row['goods_id']]['goods_name']       = $row['goods_name'];
		$arr[$row['goods_id']]['cat_id']           = $row['cat_id'];
        $arr[$row['goods_id']]['des_id']           = $row['des_id'];
        $arr[$row['goods_id']]['cat2_id']		   = $row['cat2_id'];
		$arr[$row['goods_id']]['act_id']		   = $row['act_id'];
		$arr[$row['goods_id']]['type']			   = $row['type'];
        $arr[$row['goods_id']]['goods_thumb']      = get_image_path($row['goods_id'], $row['goods_thumb'], true);
        $arr[$row['goods_id']]['goods_img']        = get_image_path($row['goods_id'], $row['goods_img']);
        $arr[$row['goods_id']]['url']              = 'goods2.php?id='.$row['goods_id'];
		//$arr[$row['goods_id']]['cat_name']		   = get_cat_name($row['cat_id']);
    }

    return $arr;
}
/**
 * 获得套装稿件列表
 *
 * @access  public
 * @param   string  $children
 * @return  array
 */
function category_get_goods1($act_id = '', $size = 200 , $page = 1)
{
    $display = $GLOBALS['display'];

    /* 获得商品列表 */
	$sql  = "SELECT g.goods_id,g.cat_id,g.des_id,g.cat2_id ,g.act_id,g.type,g.promote_price,g.shop_price,g.market_price,g.goods_name, g.goods_thumb , g.goods_img ,gg.package_id FROM ". 
		$GLOBALS['ecs']->table('goods') . " as g, ".$GLOBALS['ecs']->table('goods_aa_data')." as gg WHERE g.goods_id = gg.goods_id AND is_delete = 0 AND type = 1 AND gg.package_id = '$act_id' ORDER BY goods_id ASC";

    $res = $GLOBALS['db']->selectLimit($sql, $size, ($page - 1) * $size);

    $arr = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
		$arr[$row['goods_id']]['goods_id']         = $row['goods_id'];
        $arr[$row['goods_id']]['goods_name']       = $row['goods_name'];
		$arr[$row['goods_id']]['cat_id']           = $row['cat_id'];
        $arr[$row['goods_id']]['des_id']           = $row['des_id'];
        $arr[$row['goods_id']]['cat2_id']		   = $row['cat2_id'];
		$arr[$row['goods_id']]['package_id']	   = $row['package_id'];
		$arr[$row['goods_id']]['type']			   = $row['type'];
        $arr[$row['goods_id']]['goods_thumb']      = get_image_path($row['goods_id'], $row['goods_thumb'], true);
        $arr[$row['goods_id']]['goods_img']        = get_image_path($row['goods_id'], $row['goods_img']);
        $arr[$row['goods_id']]['url']              = 'goods2.php?id='.$row['goods_id'].'&act_id='.$row['package_id'].'&t=1';
		//$arr[$row['goods_id']]['cat_name']		   = get_cat_name($row['cat_id']);
		$arr[$row['goods_id']]['promote_price']    = $row['promote_price'];
        $arr[$row['goods_id']]['shop_price']	   = $row['shop_price'];
		$arr[$row['goods_id']]['market_price']	   = $row['market_price'];
		
    }

    return $arr;
}

/**
 * 获得对比稿件列表
 *
 * @access  public
 * @param   string  $children
 * @return  array
 */
function category_get_goods2($goods_ids,$page = 1,$size= 10)
{
    $display = $GLOBALS['display'];
    $where = "g.is_delete = 0 AND type = 1 AND goods_id in(".$goods_ids.") ";

    /* 获得商品列表 */
    $sql = 'SELECT g.goods_id,g.cat_id,g.des_id,g.cat2_id ,g.act_id,g.type, g.goods_name, g.goods_thumb , g.goods_img ' .
            'FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' .
            "WHERE $where  ORDER BY goods_id ASC";
    $res = $GLOBALS['db']->selectLimit($sql, $size, ($page - 1) * $size);

    $arr = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
		$arr[$row['goods_id']]['goods_id']         = $row['goods_id'];
        $arr[$row['goods_id']]['goods_name']       = $row['goods_name'];
		$arr[$row['goods_id']]['cat_id']           = $row['cat_id'];
        $arr[$row['goods_id']]['des_id']           = $row['des_id'];
        $arr[$row['goods_id']]['cat2_id']		   = $row['cat2_id'];
		$arr[$row['goods_id']]['act_id']		   = $row['act_id'];
		$arr[$row['goods_id']]['type']			   = $row['type'];
        $arr[$row['goods_id']]['goods_thumb']      = get_image_path($row['goods_id'], $row['goods_thumb'], true);
        $arr[$row['goods_id']]['goods_img']        = get_image_path($row['goods_id'], $row['goods_img']);
        $arr[$row['goods_id']]['url']              = 'goods2.php?id='.$row['goods_id'].'&t=2';
		//$arr[$row['goods_id']]['cat_name']		   = get_cat_name($row['cat_id']);
    }

    return $arr;
}

/*获取对比稿件的ID*/
function getCookie_goods(){
	//cookie 保存的对比数据
	$json = str_replace('\\',"",$_COOKIE['compareItems']);
	$g2 = json_decode($json);
	$goods_ids = '';
	if(count($g2)>0){
		$k = 0 ;
		foreach($g2 as $key=>$value){
			if($k==0){
				$goods_ids .= $key;
			}else{
				$goods_ids .= ','.$key;
			}
			$k++;
		}
	}
	return $goods_ids;
}

function getCatParentID($cat_id){
	$sql = "select parent_id FROM ". $GLOBALS['ecs']->table('category')." WHERE cat_id='".$cat_id."'";
	return $GLOBALS['db']->getOne($sql);
}

function get_act_type($act_id){
	$sql = "select act_type FROM ". $GLOBALS['ecs']->table('goods_aa')." WHERE act_id='".$act_id."'";
	return $GLOBALS['db']->getOne($sql);
}

/*商品组合列表*/
function get_package_list( $act_type = 0,$goods_aa_list = array()){
	$sql = "SELECT act_id,act_name FROM " . $GLOBALS['ecs']->table('goods_activity'). " WHERE `act_type` = ". GAT_PACKAGE ." AND act_type2 = '$act_type' ORDER BY act_id DESC";
	$res = $GLOBALS['db']->query($sql);

	$list = array();
	while ($row = $GLOBALS['db']->fetchRow($res))
	{	
		$row['goods_list'] = get_package_goodslist($row['act_id'] , $goods_aa_list );
		$list[] = $row;
	}
	return $list;
}
/*组合的商品列表 整合稿件进去*/
function get_package_goodslist($act_id = 0 , $goods_aa_list = array() ){
	$goods_list = get_package_goods_list( $act_id );
	$list = array();
	foreach($goods_list as $row){
			foreach($goods_aa_list as $goods2){
				if($row['cat_id'] == $goods2['cat_id']){
					$row['goods_thumb'] = $goods2['goods_thumb'];
					$row['goods2_id']	= $goods2['goods_id'];
					$row['shop_price']	= $goods2['shop_price'];
					$row['market_price']	= $goods2['market_price'];
					$row['promote_price']	= $goods2['promote_price'];
					if($goods2['promote_price'] > 0){
						$row['format_promote_price']   = price_format($goods2['promote_price']);
					}else{
						$row['format_promote_price']   = '';
					}
					$row['format_market_price']   = price_format($goods2['market_price']);
					$row['format_shop_price']     = price_format($goods2['shop_price']);
					
				}
			}
		$list[] = $row;
	}
	return $list;
}

/* 组合的商品列表*/
function get_package_goods_list($package_id)
{
    $sql = "SELECT pg.package_id,g.goods_id,g.cat_id,g.goods_name
            FROM " . $GLOBALS['ecs']->table('package_goods') . " AS pg, " .$GLOBALS['ecs']->table('goods') . " AS g WHERE pg.goods_id = g.goods_id
            AND pg.package_id = '$package_id'";
    $resource = $GLOBALS['db']->getAll($sql);
    if (!$resource)
    {
        return array();
    }

    $list = array();
	foreach($resource as $key=>$data){
		$list[$key]['package_id']	= $data['package_id'];
		$list[$key]['goods_id']		= $data['goods_id'];
		$list[$key]['cat_id']		= $data['cat_id'];
		$list[$key]['goods_name']	= $data['goods_name'];
	}
    return $list;
}


?>